﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Data;
using AG.MapEditor.Data;

namespace AGMapEditor.Windows
{
    public partial class AGEditObjectWindow : Form
    {
        private AGMapObject _mapObject;
        private AGUnit _unit;
        private AGModel _model;
        private AGModelCategory _modelCategory;

        public AGEditObjectWindow(AGMapObject mapObject)
        {
            InitializeComponent();

            _mapObject = mapObject;

            BindUI();
        }

        private void BindUI()
        {
            _ctlEditId.Text = _mapObject.Id.ToString();
            _ctlEditCaption.Text = _mapObject.Caption;
            _ctlEditPosRow.Text = _mapObject.PosRow.ToString();
            _ctlEditPosCol.Text = _mapObject.PosCol.ToString();

            for (int index = 0; index < AGMEContext.Current.Data.ObjectCategoryList.Count; index++)
            {
                _ctlListCategory.Items.Add(AGMEContext.Current.Data.ObjectCategoryList[index]);
                if (AGMEContext.Current.Data.ObjectCategoryList[index].Id == _mapObject.CategoryId)
                {
                    _ctlListCategory.SelectedIndex = index;
                }
            }

            for (int index = 0; index < AGMEContext.Current.Data.UnitList.Count; index++)
            {
                if (AGMEContext.Current.Data.UnitList[index].Id == _mapObject.UnitId)
                {
                    _unit = AGMEContext.Current.Data.UnitList[index];
                    break;
                }
            }

            for (int index = 0; index < AGMEContext.Current.Data.ModelList.Count; index++)
            {
                if (AGMEContext.Current.Data.ModelList[index].Id == _unit.ModelId)
                {
                    _model = AGMEContext.Current.Data.ModelList[index];
                    int categoryId = _model.CategoryId;
                    _model = AGMEContext.Current.Resource.GetModel(_model.Id);
                    _model.CategoryId = (ushort)categoryId;
                    break;
                }
            }

            for (int index = 0; index < AGMEContext.Current.Data.ModelCategoryList.Count; index++)
            {
                AGModelCategory modelCategory = AGMEContext.Current.Data.ModelCategoryList[index];
                if (modelCategory.Id == _model.CategoryId)
                {
                    _modelCategory = modelCategory;
                    break;
                }
            }

            for (int index = 0; index < _modelCategory.Actions.Count; index++)
            {
                AGModelCategoryAction action = _modelCategory.Actions[index];
                _ctlListAction.Items.Add(action);
                if (action.Id == _mapObject.ActionId)
                {
                    _ctlListAction.SelectedIndex = index;
                }
            }

            for (int index = 0; index < _modelCategory.Directions.Count; index++)
            {
                AGModelCategoryDirection direction = _modelCategory.Directions[index];
                _ctlListDirection.Items.Add(direction);
                if (direction.Id == _mapObject.DirectionId)
                {
                    _ctlListDirection.SelectedIndex = index;
                }
            }
        }

        private void _ctlBtnOK_Click(object sender, EventArgs e)
        {
            _mapObject.Id = (uint)Convert.ToInt32(_ctlEditId.Text);
            _mapObject.Caption = _ctlEditCaption.Text;
            _mapObject.PosRow = (ushort)Convert.ToInt16(_ctlEditPosRow.Text);
            _mapObject.PosCol = (ushort)Convert.ToInt16(_ctlEditPosCol.Text);

            AGMapObjectCategory selCategory = _ctlListCategory.SelectedItem as AGMapObjectCategory;
            _mapObject.CategoryId = selCategory.Id;

            

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
